<template>
  <div class="search-wapper">
    <van-nav-bar
      title="执行指令"
      left-text="返回"
      left-arrow
      fixed
      @click-left="onClickLeft"
    >
      <van-icon name="arrow-left" slot="left" />
      <!--<van-icon name="search" slot="right"/>-->
    </van-nav-bar>
    <div class="search-content">
      <div class="search-content-head">
        <div class="custom-title1">
          <span class="user-line-input">{{ taskcodeId }}</span>
          <span class="user-line-label">
            <!-- <van-count-down :time="time"/> -->
            <CountDown></CountDown>
          </span>
        </div>

        <van-cell-group>
          <van-field
            v-model="userInfo.itemid"
            label="物资号"
            placeholder="请输入物资号"
          >
            <van-button
              slot="button"
              size="small"
              type="primary"
              @click="scancode(0)"
              >扫一扫</van-button
            >
          </van-field>
          <van-field
            v-model="userInfo.locationid"
            label="地址"
            placeholder="请输入地址编号"
          >
            <van-button
              slot="button"
              size="small"
              type="primary"
              @click="scancode(1)"
              >扫一扫</van-button
            >
          </van-field>
          <!-- <van-field
                            v-model="userInfo.prior"
                            label="任务等级"
                            @click="clickRight"
                            placeholder="请选择任务等级" disabled="false"
                    /> -->
        </van-cell-group>
      </div>

      <div class="button-content">
        <van-button
          type="primary"
          round
          style="width: 100%"
          @click="taskClick"
          v-if="flag == 0"
          >{{ buttonName }}
        </van-button>
        <van-button
          type="warning"
          round
          style="width: 100%"
          @click="taskClick"
          v-if="flag == 1"
          >{{ buttonName }}
        </van-button>
      </div>
    </div>

    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" />

    <!--<van-action-sheet v-model="show" :actions="actions" @select="onSelect"/>-->
  </div>
</template>

<script>
import { startOrEndTask } from "@/api/Invtask.js";
import { Dialog } from "vant";
export default {
  name: "ExecutionInstruction",
  components: {
    CountDown: () => import("@/components/CountDown.vue")
  },
  data() {
    return {
      userInfo: {},
      show: false,
      taskcodeId: "",
      buttonName: "开始任务",
      flag: 0,
      actions: [
        { name: "10" },
        { name: "9" },
        { name: "8" },
        { name: "7" },
        { name: "6" },
        { name: "5" },
        { name: "4" },
        { name: "3" },
        { name: "2" },
        { name: "1" },
        { name: "0" }
      ],
      time: 24 * 60 * 60 * 1000
    };
  },
  methods: {
    onClickLeft() {
      
     // this.$router.back(-1);
      this.$router.push({
           path: "/order/action"
      })
    },
    taskClick() {
      if (this.userInfo.itemid == null) {
        Dialog.alert({
          title: "执行指令",
          message: "物资id不能为空"
        }).then(() => {
          // on close
        });
        return;
      }

      if (this.flag == 0) {
        this.buttonName = "结束任务";
        this.flag = 1;
        this.userInfo.type = 2;
        if (this.userInfo.locationid == null) {
          Dialog.alert({
            title: "执行指令",
            message: "地址编号不能为空"
          }).then(() => {
            // on close
          });
          return;
        }

        startOrEndTask(this.userInfo).then(res => {
          this.$router.push({
            path: "/order"
          });
        });
      } else {
        this.buttonName = "开始任务";
        this.flag = 0;
        this.userInfo.type = 1;
        startOrEndTask(this.userInfo).then(res => {});
      }
    },
    onSelect(item) {
      // 默认情况下，点击选项时不会自动关闭菜单
      // 可以通过 close-on-click-action 属性开启自动关闭
      this.show = false;
      // Toast(item.name);
      console.log("onSelect======", item.name);
      this.userInfo.prior = item.name;
    },
    clickRight() {
      console.log("=====clickRight===");
      this.show = true;
    },
    scancode(stu) {
      if (stu == 0) {
        this.$router.push({
          path: "/scancode",
          query: { params: "itemid" }
        });
      } else if (stu == 1) {
        this.$router.push({
          path: "/scancode",
          query: { params: "locationid" }
        });
      }
    }
  },
  created() {
    if (this.$route.query.taskcodeId != null) {
      this.taskcodeId = this.$route.query.taskcodeId;
    }
    if (this.$route.query.itemid != null) {
      this.userInfo.itemid = this.$route.query.itemid;
    }
    if (this.$route.query.locationid != null) {
      this.userInfo.locationid = this.$route.query.locationid;
    }
  }
};
</script>

<style scoped lang="scss">
.search-wapper {
  position: relative;
  .search-content {
    position: relative;
    /*top: 46px;*/
    /*height: calc(100% - 50px);*/
    /*margin-bottom: 10px;*/
    top: 35%; /*偏移*/
    transform: translateY(-50%);
    .search-content-head {
      margin: 20px;
      padding: 5px;
      background: #fff;
      border-radius: 8px;
    }
    .custom-title1 {
      color: #000;
      background-color: #fff;
      padding: 30px 16px;
      span {
        font-size: 14px;
      }
    }
    .user-line-label {
      width: 85px;
      text-align: right;
      font-size: 14px;
      color: #606266;
      line-height: 1;
      /*padding: 0px 12px 10px 0;*/
      box-sizing: border-box;
      display: inline-block;
    }
    .user-line-input {
      position: relative;
      display: inline-block;
      font-size: 14px;
      vertical-align: top;
      width: calc(100% - 85px);
      font-weight: bold;
    }
  }
  .button-content {
    position: relative;
    /*top: 46px;*/
    margin-top: 20px;
    padding: 0px 20px;
  }
}
</style>
